package demo.controller;

import demo.dao.UserMapper;
import jdbc.DBUtil;
import jdbc.InputUtil;
import jdbc.UserInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/*
控制器
用于根据用户的不同需求，调用不同业务
 */
public class UserController {
    private UserMapper userMapper = new UserMapper();
    public void start(){
        System.out.println("欢迎使用");
        System.out.println("请输入要操作的编号:");
        System.out.println("[1]:用户注册");
        System.out.println("[2]:用户登录");
        System.out.println("[3]:修改密码");
        Scanner scanner=new Scanner(System.in);
        String num=scanner.nextLine();
        switch (num){
            case "1":
                regUser();
                break;
            case "2":
                loginUser();
                break;
            case "3":
                changePWD();
                break;
            default:
                System.out.println("输入编号有误");
        }

    }
    //注册用户功能
    public void regUser(){
        UserInfo userInfo=InputUtil.getInputObject(new UserInfo(),"欢迎注册","注册");
        UserMapper userMapper=new UserMapper();
        if (userMapper.findByUsername(userInfo.getUsername())!=null){
            System.out.println("用户名已存在");
        }else{
            userMapper.insert(userInfo);
            System.out.println("注册成功");
        }
        //用户名不能重复
//        UserInfo userInfo= InputUtil.getInputObject(new UserInfo(),"欢迎注册");
//        try ( Connection connection= DBUtil.getConnection()){
//            PreparedStatement ps = connection.prepareStatement(
//                    "SELECT username,password,nickname,age " +
//                            "FROM userinfo " +
//                            "WHERE username=?");
//            ps.setString(1,userInfo.getUsername());
//            ResultSet rs = ps.executeQuery();
//            if(rs.next()){
//                System.out.println("用户名重复");
//            }else{
//                ps=connection.prepareStatement(
//                        "INSERT INTO userinfo(username,password,nickname,age) " +
//                                "VALUES(?,?,?,?)"
//                );
//                ps.setString(1,userInfo.getUsername());
//                ps.setString(2,userInfo.getPassword());
//                ps.setString(3,userInfo.getNickname());
//                ps.setInt(4,userInfo.getAge());
//                ps.executeUpdate();
//                System.out.println("注册成功");
//
//            }
//
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
    }
    //用户登录功能
    public void loginUser(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎登录");
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        UserInfo userInfo=userMapper.findByUsername(username);
        if (userInfo==null||!userInfo.getPassword().equals(password)){
            System.out.println("用户名或密码不正确，登录失败");
        }else{
            System.out.println("登录成功,欢迎回来："+userInfo.getNickname());
        }
    }
    //修改密码功能
    public void changePWD(){
        /*
            首先要求用户输入三个信息:
            1:用户名，旧密码，新密码
            2:根据用户名和旧密码判断输入是否正确
            3:如果旧密码正确则将新密码进行修改，否则提示用户输入有误
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("修改密码");
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入旧密码:");
        String oldPassword = scanner.nextLine();
        System.out.println("请输入新密码:");
        String newPassword = scanner.nextLine();



        UserInfo userInfo = userMapper.findByUsername(username);
        if (userInfo==null||!userInfo.getPassword().equals(oldPassword)){
            System.out.println("输入信息有误，修改失败");
        }
        else{
            userInfo.setPassword(newPassword);
            userMapper.update(userInfo);
            System.out.println("修改成功");
        }

//
//        try (Connection connection = DBUtil.getConnection();){
//            PreparedStatement ps = connection.prepareStatement(
//                    "SELECT username,password,nickname,age " +
//                            "FROM userinfo " +
//                            "WHERE username=? AND password=?"
//            );
//            ps.setString(1,username);
//            ps.setString(2,oldPassword);
//            ResultSet rs = ps.executeQuery();
//            if(rs.next()){
//                //旧密码输入正确
//                PreparedStatement ps2 = connection.prepareStatement(
//                        "UPDATE userinfo " +
//                                "SET password=? " +
//                                "WHERE username=?"
//                );
//                ps2.setString(1,newPassword);
//                ps2.setString(2,username);
//                int num = ps2.executeUpdate();
//                if(num>0){
//                    System.out.println("密码修改成功");
//                }
//            }else{
//                System.out.println("用户名或旧密码错误");
//            }
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
    }
    public static void main(String[] args) {
        UserController userController=new UserController();
        userController.start();

    }
}
